package getsmart.project.database;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBVideo {
	private static final String ROW_ID = "_id";
	private static final String VIDEONAME = "NamaVideo";
	private static final String FILENAME = "namaFile";
	private static final String LOCATIONNAME = "lokasiFile";
	private static final String DESCRIPTIONVIDEO = "deskripsiVideo";
	
 
	private static final String NAMA_DB = "VideoDB";// nama database
	private static final String NAMA_TABEL = "Video";// nama tabel
	private static final int DB_VERSION = 1;
 
	private static final String CREATE_TABLE_PROFILE = "create table "+NAMA_TABEL+" ("+ROW_ID+" integer PRIMARY KEY autoincrement, "+VIDEONAME+" text, "+FILENAME+" text, "+LOCATIONNAME+" text, "+DESCRIPTIONVIDEO+" text)";
	private final Context context;
	private DatabaseOpenHelper dbHelper;
	private SQLiteDatabase db;
 
	public DBVideo(Context ctx) {
		this.context = ctx;
		dbHelper = new DatabaseOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}
 
	private static class DatabaseOpenHelper extends SQLiteOpenHelper {
		 
		public DatabaseOpenHelper(Context context) {
			super(context, NAMA_DB, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_TABLE_PROFILE);
		}
 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+NAMA_TABEL);
			onCreate(db);
 
		}
	}
 
	public void close() {
		dbHelper.close();
	}
 
	public void addProfile(String namaVideo,String namaFile,String lokasi,String deskripsiVideo) {
 
		ContentValues values = new ContentValues();
		values.put(VIDEONAME, namaVideo);
		values.put(FILENAME, namaFile);
		values.put(LOCATIONNAME, lokasi);
		values.put(DESCRIPTIONVIDEO, deskripsiVideo);
 
		try {
 
		//	db.delete(NAMA_TABEL, null, null);
			db.insert(NAMA_TABEL, null, values);
		} catch (Exception e) {
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}
	}
	

	

 
	
	public ArrayList<ArrayList<Object>> getAllVideoRecord(){
		ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { ROW_ID, VIDEONAME,FILENAME,LOCATIONNAME,DESCRIPTIONVIDEO }, null, null,
					null, null, null);
			cur.moveToFirst();
			if (!cur.isAfterLast()) {
				do {
					ArrayList<Object> dataList = new ArrayList<Object>();
					dataList.add(cur.getLong(0));
					dataList.add(cur.getString(1));
					dataList.add(cur.getString(2));
					dataList.add(cur.getString(3));
					dataList.add(cur.getString(4));
 
					dataArray.add(dataList);
 
				} while (cur.moveToNext());
 
			}
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return dataArray;
	}
	public String getProfilName(){
		String data = "";
		return data;
	}

	
	public String getProfilName(int id){
		String name = "";
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { ROW_ID, VIDEONAME,FILENAME,LOCATIONNAME },  ROW_ID + "=" + '"' + id + '"', null,
					null, null, null);
			cur.moveToFirst();
            if(cur.moveToFirst()) {
                name = cur.getString(cur.getColumnIndexOrThrow(VIDEONAME));
        }
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return name;
	}
	public String getDescriptionVideo(int idVideo){
		String desVideo = "";
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { ROW_ID, VIDEONAME,FILENAME,LOCATIONNAME,DESCRIPTIONVIDEO },  ROW_ID + "=" + '"' + idVideo + '"', null,
					null, null, null);
			cur.moveToFirst();
            if(cur.moveToFirst()) {
            	desVideo = cur.getString(cur.getColumnIndexOrThrow(DESCRIPTIONVIDEO));
        }
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return desVideo;
	}
	public String getVideoName(int idVideo){
		String namaVideo = "";
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { ROW_ID, VIDEONAME,FILENAME,LOCATIONNAME,DESCRIPTIONVIDEO },  ROW_ID + "=" + '"' + idVideo + '"', null,
					null, null, null);
			cur.moveToFirst();
            if(cur.moveToFirst()) {
            	namaVideo = cur.getString(cur.getColumnIndexOrThrow(FILENAME));
        }
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		
		return namaVideo;
	}
	
	public void generateDataVideo(){
		ContentValues values = new ContentValues();
		values.put(VIDEONAME, "Les Vokal 1");
		values.put(FILENAME, "test10.wmv");
		values.put(LOCATIONNAME, "");
		values.put(DESCRIPTIONVIDEO, "Video ini berisi tentang cara bernyanyi yang baik dan benar");
		db.insert(NAMA_TABEL, null, values);
	}
}

